package game;



import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class Launch extends Canvas implements KeyListener, ActionListener, Runnable {

	private static final long serialVersionUID = 1L;
	private BufferStrategy strategy;
	static Launch m;
	static JFrame frame;
	public JButton p,e,map2,map3 ;
	public static JPanel panel;
	public static JLabel label;
	private static final int CLEAR = 0;

	private static final int BLOCKED = 1;

	private static final int WIDTH = 16;

	private static final int HEIGHT = 16;

	public static final int TILE_SIZE = 35;
	private boolean left;

	private boolean right;

	private boolean up;

	private boolean down;

	public static int[][] data = new int[WIDTH][HEIGHT];
	private Entity player;;
	BufferedImage img = null;
	static Thread t;
	
/*	public Launch() {
		m = new Launch();
		m.map();
		m.frame();
		t = new Thread(m);
	}
*/
	public void fix() {
		m = new Launch();
		m.map();
		m.frame();
		t = new Thread(m);
	}
	public void map() {

		for (int y = 0; y < HEIGHT; y++) {
			data[0][y] = CLEAR; 
			data[3][y] = BLOCKED;
			data[7][y] = BLOCKED;
			data[11][y] = BLOCKED;
			data[WIDTH - 1][y] = BLOCKED;
		}
		for (int x = 0; x < WIDTH; x++) {
			if ((x > 0) && (x < WIDTH - 1)) {
				data[x][10] = CLEAR;
			}

			if (x > 2) {
				data[x][9] = BLOCKED;
			}
			data[x][0] = BLOCKED;
			data[x][HEIGHT - 1] = BLOCKED;
		}

		data[4][9] = CLEAR;
		data[7][5] = CLEAR;
		data[7][4] = CLEAR;
		data[11][7] = CLEAR;
		data[15][2]=CLEAR;
		data[15][14]=CLEAR;

	}

	public void paint(Graphics g)

	{

		Graphics2D g1 = (Graphics2D) (g);

	//	Image a = new ImageIcon("3.png").getImage();
	//	g1.drawImage(a, 560, 0, this);
		g1.translate(0, 0);

		for (int x = 0; x < WIDTH; x++) {
			for (int y = 0; y < HEIGHT; y++) {

				g1.setColor(Color.GREEN);
				if (data[x][y] == BLOCKED) {
					g1.setColor(Color.red);
				}

				g1.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
				g1.setColor(g.getColor().darker());
				g1.drawRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
			}
		}
	}

	public boolean blocked(float x, float y) {
		

		return data[(int) x][(int) y] == BLOCKED;
		
	}

	public void frame() {
		frame = new JFrame("Frame for Game");
		frame.setSize(1000, 700);
		frame.setBackground(Color.black);
		frame.setResizable(false);
		frame.add(m);

		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel = (JPanel) frame.getContentPane();
		panel.setPreferredSize(new Dimension(1000, 700));
		panel.setLayout(null);

		setBounds(0, 0, 1000, 550);
		panel.add(this);

		p = new JButton("Start");
	//	e = new JButton("ENEMY");
	//	map2=new JButton("MAP2");
		p.setBounds(10, 600, 80, 30);
	//	e.setBounds(90, 600, 80, 30);
    //    map2.setBounds(170,600,80,30);
    //    panel.add(map2);
		panel.add(p);
	//	panel.add(e);
		//panel.add();
		
		createBufferStrategy(2);
		strategy = getBufferStrategy();

		p.addActionListener(m);
	//	e.addActionListener(m);
		addKeyListener(this);
		// gameLoop();
	//	t = new Thread(m);
	}

	// public void gameLoop() {

	// }

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == p)

			try {
				img = ImageIO.read(new File("player.jpg"));

				player = new Entity(img, m, 1.5f, 1.5f);

				t.start();
				// gameLoop();
			} catch (IOException e) {
				System.err.println("Unable to load sprite: res/sprite.gif");
				System.exit(0);

			}

	}

	@Override
	public void run() {

		boolean gameRunning = true;
		long last = System.nanoTime();

		while (gameRunning) {

			Graphics2D gg = (Graphics2D) strategy.getDrawGraphics();

			m.paint(gg);

			player.paint(gg);

			gg.dispose();
			strategy.show();

			try {
				Thread.sleep(4);
			} catch (Exception e) {
			}
			;

			long delta = (System.nanoTime() - last) / 1000000;
			last = System.nanoTime();

			for (int i = 0; i < delta / 5; i++) {
				logic(5);
			}

			if ((delta % 5) != 0) {
				logic(delta % 5);
			}
		}

	}

	public void logic(long delta) {

		float dx = 0;
		float dy = 0;
		if (left) {
			dx -= 1;
			System.out.println(dx);
		}
		if (right) {
			dx += 1;
		}
		if (up) {
			dy -= 1;
		}
		if (down) {
			dy += 1;
		}

		if ((dx != 0) || (dy != 0)) {
			player.move(dx * delta * 0.003f, dy * delta * 0.003f);
			System.out.println("after="+dx );
			
		}
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyPressed(KeyEvent e) {

		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			left = true;
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			right = true;
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			down = true;
		}
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			up = true;
		}
	}

	public void keyReleased(KeyEvent e) {

		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			left = false;
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			right = false;
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			down = false;
		}
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			up = false;
		}
	}

/*	public static void main(String[] args) {
		m = new Launch();
		m.map();
		m.frame();
		t = new Thread(m);
	}
*/
}
